package day9;

import javax.lang.model.element.VariableElement;
import java.util.*;

public class exe extends Thread {
    static double balance=10000;

    //static int[] arr = {10,5,20,50,100,200,500,800,2,80,300};
    public synchronized static void deposit(int amount){//存款
        if(amount<=0){
            System.out.println("请存入正数的金额");
        }else{
            balance+=amount;
            System.out.println("您已成功存入"+amount+",当前余额"+balance);
        }

    }
    public synchronized static void withdraw(int amount){//取款
        //BankAccount b=new BankAccount(1000);
        if(amount>=balance){
            System.out.println("您的余额不足！");
        }else{
            balance-=amount;
            System.out.println("您已成功取走"+amount+",还剩余额"+balance);
        }
    }

    public static void main(String[] args) {
        //demo1();
        //demo2();
        demo3();
    }
    public static void demo1(){
        Dem t1 = new Dem();
        t1.setName("A");
        Dem t2 = new Dem();
        t2.setName("B");
        Dem t3 = new Dem();
        t3.setName("C");
        t1.start();
        t2.start();
        t3.start();
    }
    public static void demo2(){
        new Thread(new BankAccount(),"用户1").start();
        new Thread(new BankAccount(),"用户2").start();
        new Thread(new BankAccount(),"用户3").start();
    }
    public static void demo3(){
        Thread t1=new Thread(new Money(),"抽奖箱1");
        Thread t2=new Thread(new Money(),"抽奖箱2");
        t1.start();
        t2.start();
    }
}
class BankAccount implements Runnable{
    static double balance;

//    public BankAccount(double balance) {
//        this.balance = balance;
//    }
    public void run() {
        Random r=new Random();
        while (true){
            int i=r.nextInt(2);
            if(i==0){
                exe.deposit(r.nextInt(1000));//存款
            }else {
                exe.withdraw(r.nextInt(200));//取款
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
class Dem extends Thread{
    static int sum=100;//一共有100张票
    public void run() {
        String name=Thread.currentThread().getName();
        while (true) {
            synchronized (Dem.class) {
                if(sum <= 0) {
                        System.out.println("票已售空！");
                        break;
                    }
                System.out.println(name+"卖出第"+(100-sum+1)+"张票");
                sum--;
            }
        }
    }
}
class Money implements Runnable{
    static Integer[] arr = {10,5,20,50,100,200,500,800,2,80,300};
    static List<Integer> list=new LinkedList<Integer>(Arrays.asList(arr));
    public void run() {
        String name=Thread.currentThread().getName();
        Random r=new Random();
        while (true){
            synchronized (Money.class){
                if(list.isEmpty()){
                    System.out.println("奖池已空");
                    return;
                }
                //int arrIndex=r.nextInt(arr.length);
                //int result=arr[arrIndex];
                int arrIndex=r.nextInt(list.size());//下标
                int result=list.get(arrIndex);
                list.remove(arrIndex);
                System.out.println(name+"又产生了"+result+"元大奖");
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}